iT邦幫忙

2025 iThome 鐵人賽

DAY 14
0
DevOps

Mac 環境 n8n 30 天學習計畫系列 第 14

Day 14 — 呼叫天氣 API → Console🌥️

  • 分享至 

  • xImage
  •  

本次運用能在 n8n(Mac 本機)上直接執行的詳細步驟。採用 Open-Meteo(免費、無 API key) 的流程範例:
Manual Trigger → Set(輸入城市或座標)→ HTTP Request(地理編碼,可選)→ HTTP Request(取得天氣)→ Function(格式化),最終輸出到 Console(Execution Log)。
https://ithelp.ithome.com.tw/upload/images/20250927/20169144fGhqRkWe8F.png

為什麼選 Open-Meteo?

  • 免費、無需註冊或 API key(方便鐵人賽快速示範)。
  • 支援地理編碼(查 city → 回 lat/lon)與天氣資料(current_weather、hourly 等)。

一、整體工作流程(快速總覽)

  1. Manual Trigger(方便手動測試)
  2. Set(放要查的 city,例如 Taipei,或直接放 latitude / longitude
  3. HTTP Request(Geocoding,可選)https://geocoding-api.open-meteo.com/v1/search?name={{city}}&count=1 → 取得 lat/lon
  4. HTTP Request(Weather):使用 lat/lon 呼叫 Open-Meteo 天氣 API,取得 JSON
  5. Function:把 JSON 轉成可讀訊息(例如:台北 現在溫度 25°C,風速 3.5 m/s)並回傳(在 Execution Log 查看)

二、實作步驟(非常詳細)

0) 先準備、啟動 n8n

在 Terminal 執行:

n8n start

1) 新增 Workflow

  • Workflows → New,命名:Day14_Weather_API

2) 加入 Manual Trigger(測試)

  • 畫布上按 + → 搜尋 Manual Trigger,新增。
    (等到要自動化時可換成 Cron node)

3) 加入 Set 節點(提供查詢條件)

  • 在 Manual Trigger 右側 + → 搜尋 Set,新增節點名稱 Input

  • Values 加欄位:

    • latitudelongitude(Number)例如:25.0330, 121.5654
  • 儲存。
    https://ithelp.ithome.com.tw/upload/images/20250927/201691440t4MoIUIGC.png


4) 取得天氣:HTTP Request(Open-Meteo)

  1. 新增一個 HTTP Request node,命名 Get Weather

  2. 設定 URL(使用 Expression):

    https://api.open-meteo.com/v1/forecast?latitude={{ $json["latitude"] }}&longitude={{ $json["longitude"] }}&current_weather=true&timezone=Asia%2FTaipei
    

https://ithelp.ithome.com.tw/upload/images/20250927/20169144W4kMaDzr1T.png
說明:current_weather=true 會回傳 current_weather 物件(含 temperature, windspeed, winddirection, weathercode)。timezone 指輸出時間區域。
3. HTTP Method:GET
4. 連線:如果用 Geocoding,請把 GeocodingGet Weather 連起來;若直接用 Set,連 InputGet Weather
5. 點 Execute Node 測試。你會在 Output 頁籤看到 JSON,範例(節錄):

   [
  {
    "latitude": 25,
    "longitude": 121.625,
    "generationtime_ms": 0.03039836883544922,
    "utc_offset_seconds": 28800,
    "timezone": "Asia/Taipei",
    "timezone_abbreviation": "GMT+8",
    "elevation": 12,
    "current_weather_units": {
      "time": "iso8601",
      "interval": "seconds",
      "temperature": "°C",
      "windspeed": "km/h",
      "winddirection": "°",
      "is_day": "",
      "weathercode": "wmo code"
    },
    "current_weather": {
      "time": "2025-09-27T20:15",
      "interval": 900,
      "temperature": 28,
      "windspeed": 7.6,
      "winddirection": 87,
      "is_day": 0,
      "weathercode": 1
    }
  }
]

5) 新增 Function 節點(格式化輸出到 Console)

  1. Get Weather 右側新增 Function 節點,命名 Format Message
  2. 在 Function 裡貼以下程式碼(把 API 回傳的 JSON 轉成易讀訊息):
// 取第一筆 item(HTTP Request 傳回的資料)
return items.map(item => {
  const data = item.json;
  const cw = data.current_weather || {};
  const temp = cw.temperature !== undefined ? `${cw.temperature}°C` : 'N/A';
  const wind = cw.windspeed !== undefined ? `${cw.windspeed} m/s` : 'N/A';
  const time = cw.time || data.time || 'N/A';
  // 你也可以根據 weathercode 顯示文字,這裡用簡單示範
  const msg = `地點: (${data.latitude}, ${data.longitude})\n時間: ${time}\n現在溫度: ${temp}\n風速: ${wind}`;
  return {
    json: {
      message: msg,
      raw: data
    }
  };
});

https://ithelp.ithome.com.tw/upload/images/20250927/20169144eTqaFEw7iM.png
3. 將 Get WeatherFormat Message 連起來。
4. 點 Execute Workflow
5. 在 Execution Log 裡,展開 Format Message 的 Output → JSON,你會看到 message 欄位。


6) 檢查 Execution Log(Console)

  • 點右側或上方的 Executions(或在 node 上按 Execution icon),找到剛剛的執行紀錄,點開逐節點查看 Input / Output JSON。

  • Format Messagemessage 欄會是可讀字串,例如:

    地點: (25, 121.625)
    時間: 2025-09-27T20:15
    現在溫度: 28°C
    風速: 7.6 m/s
    

https://ithelp.ithome.com.tw/upload/images/20250927/20169144CqbT9EmDi0.png


上一篇
📝 Day 13 小專案 – Trigger Cron → Sheet → Email
下一篇
Day 15:JSON 處理(解析溫度資訊)📑
系列文
Mac 環境 n8n 30 天學習計畫15
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言